<?php

namespace myshop\common\dao;

use longhu\base\BaseDao;
use myshop\system\model\SystemConfigValue;

class ConfigValueDao extends BaseDao
{
    public function setModel():string
    {
        return  SystemConfigValue::class;
    }

    public function more(array $keys = [],int $mer_id = 0)
    {
        $config = $this->fields($keys, $mer_id);
        foreach ($keys as $key) {
            if (!isset($config[$key])) $config[$key] = '';
        }
        return $config;
    }

    /**
     * @param array $keys
     * @param int $merId
     * @return array
     * @author xaboy
     * @day 2020-04-22
     */
    public function fields(array $keys, int $merId)
    {
        $result = $this->getModel()
            ->whereIn('config_key', $keys)
            ->where('mer_id', $merId)
            ->withAttr('value', function ($val, $data) {
            return json_decode($val, true);
        })->column('value', 'config_key');
        foreach ($result as $k => $val) {
            $result[$k] = json_decode($val, true);
        }
        return $result;
    }
}